package com.mxt.moviedemo.service.impl;

import com.mxt.moviedemo.dao.TicketMapper;
import com.mxt.moviedemo.dao.UserMapper;
import com.mxt.moviedemo.entity.*;
import com.mxt.moviedemo.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TicketServiceImpl implements TicketService {
    @Autowired
    private TicketMapper ticketMapper;


    @Override
    public PageResult<Ticket> getTicketByPage(PageRequest pageRequest) {
        List<Ticket> tickets = ticketMapper.getTicketByPage(pageRequest);
        // 过滤掉null元素
        List<Ticket> nonNullTicket = new ArrayList<>();
        for (Ticket ticket : tickets) {
            if (ticket != null) {
                nonNullTicket.add(ticket);
            }
        }
        int total = ticketMapper.getTotalTicket();
        return new PageResult<>(pageRequest.getPage(), pageRequest.getSize(), total, nonNullTicket);
    }

    @Override
    public PageResult<Ticket> searchTicketByKeyword(PageRequest pageRequest, String keyword) {
        List<Ticket> tickets = ticketMapper.searchTicketByKeyword(pageRequest, keyword);
        int total = ticketMapper.getTotalTicketByKeyword(keyword);
        return new PageResult<>(pageRequest.getPage(), pageRequest.getSize(), total, tickets);
    }

    @Override
    public Ticket getTicketById(int tid) {
        return ticketMapper.getTicketById(tid);
    }

    @Override
    public int saveTicket(Ticket ticket) {
        return ticketMapper.saveTicket(ticket);
    }

    @Override
    public List<Ticket> getTicketsByUsername(String username) {
        return ticketMapper.getTicketsByUsername(username);
    }

    @Override
    public PageResult<Ticket> getTicketsByPage(PageRequest pageRequest) {
        List<Ticket> tickets = ticketMapper.getTicketsByPage(pageRequest);
        long total = ticketMapper.getTotalTickets();
        return new PageResult<>(tickets, total);
    }

    @Override
    public List<Ticket> searchTicketsByName(String uname) {
        return ticketMapper.searchTicketsByName(uname);
    }

    @Override
    public int addTicket(Ticket ticket) {
        return ticketMapper.addTicket(ticket);
    }

    @Override
    public boolean updateTicket(Ticket ticket) {
        return ticketMapper.updateTicket(ticket);
    }

    @Override
    public int deleteTicket(int tid) {
        return ticketMapper.deleteTicket(tid);
    }


}